package net.goutalk.glcs.module.erpModel.caseErpCustomer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.entity.CaseErpCustomerFollow;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.mapper.CaseErpCustomerFollowMapper;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.service.ICaseErpCustomerFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.vo.CaseErpCustomerFollowInfoVo;
import net.goutalk.glcs.module.organization.entity.User;
import net.goutalk.glcs.module.system.entity.DictionaryDetail;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户跟进【case_erp_customer_follow】 服务实现类
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-11
 */
@Service
@AllArgsConstructor
public class CaseErpCustomerFollowServiceImpl extends ServiceImpl<CaseErpCustomerFollowMapper, CaseErpCustomerFollow> implements ICaseErpCustomerFollowService {

    @Override
    public List<CaseErpCustomerFollowInfoVo> info(List<CaseErpCustomerFollow> caseErpCustomerFollowList, List<DictionaryDetail> dictionaryDetailList, List<User> userList) {
        List<CaseErpCustomerFollowInfoVo> caseErpCustomerFollowInfoVoList = new ArrayList<>();
        for (CaseErpCustomerFollow caseErpCustomerFollow : caseErpCustomerFollowList) {
            CaseErpCustomerFollowInfoVo caseErpCustomerFollowInfoVo = BeanUtil.toBean(caseErpCustomerFollow, CaseErpCustomerFollowInfoVo.class);
            if (ObjectUtil.isNotEmpty(caseErpCustomerFollow.getFollowTypeId())){
                DictionaryDetail dictionaryDetail = dictionaryDetailList.stream().filter(x -> x.getId().equals(caseErpCustomerFollow.getFollowTypeId())).findFirst().orElse(new DictionaryDetail());
                if (StrUtil.isNotBlank(dictionaryDetail.getName())){
                    caseErpCustomerFollowInfoVo.setFollowTypeName(dictionaryDetail.getName());
                }
            }
            if (ObjectUtil.isNotEmpty(caseErpCustomerFollow.getCreateUserId())){

                User user = userList.stream().filter(x -> x.getId().equals(caseErpCustomerFollow.getCreateUserId())).findFirst().orElse(new User());
                if (StrUtil.isNotBlank(user.getName())){
                    caseErpCustomerFollowInfoVo.setCreateUserName(user.getName());
                }
            }
            caseErpCustomerFollowInfoVoList.add(caseErpCustomerFollowInfoVo);
        }

        return caseErpCustomerFollowInfoVoList;
    }
}
